from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QSpinBox, QApplication, QDialog, QDial, QHBoxLayout


class ZeroSpinBox(QSpinBox):
    zeros = 0
    atzero = pyqtSignal(int)  # 自定义信号

    def __init__(self, parent=None):
        super(ZeroSpinBox, self).__init__(parent)
        self.valueChanged.connect(self.checkzero)
        # 连接到槽函数

    def checkzero(self):  # 检查为0，则发送信号
        if self.value() == 0:
            self.zeros += 1
            self.atzero.emit(self.zeros)  # 槽函数发送信号


class Form(QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        dial = QDial()
        dial.setNotchesVisible(True)
        zerospinbox = ZeroSpinBox()

        layout = QHBoxLayout()
        layout.addWidget(dial)
        layout.addWidget(zerospinbox)
        self.setLayout(layout)

        dial.valueChanged.connect(zerospinbox.setValue)
        zerospinbox.valueChanged.connect(dial.setValue)
        zerospinbox.atzero.connect(self.announce)  # 对象中应用自定义信号的方法
        self.setWindowTitle("Signals and Slots")

    # @pyqtSlot(int)
    def announce(self, zeros):
        print("ZeroSpinBox has been at zero %d times" % zeros)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    form = Form()
    form.show()
    sys.exit(app.exec())
